<template>
	<view
		:class="{ 'uni-collapse-cell--disabled': disabled,'uni-collapse-cell--notdisabled': !disabled, 'uni-collapse-cell--open': isOpen,'uni-collapse-cell--hide':!isOpen }"
		class="uni-collapse-cell">
		<view :class="{ 'uni-collapse-cell--disabled': disabled}" class="uni-collapse-cell__title">
			<view class="uni-collapse-cell__l">
				<view class="uni-collapse-cell__title-text">{{ title }}</view>
			</view>
			<view class="uni-collapse-cell__r" :style="{height: isOpen?'':rowHeight+'px'}">
				<image class="icon-add" @click.stop="onClickAdd(obj.id)" :src="$mAssetsPath.icon_add3"></image>
				<image class="icon-bj" @click.stop="onClickEdit(obj)" :src="$mAssetsPath.icon_bianji"></image>
				<image class="icon-sc" @click.stop="onClickDel(obj.id)" :src="$mAssetsPath.icon_shanchu"></image>
				<slot name="rowIcons" :obj1="obj" :inde="listType"></slot>
				<!-- <view class="icons" :data-id="obj.id"
					:data-type="listType" @touchstart="drag.touchstart"
					@touchmove="drag.touchmove" @touchend="drag.touchend"
					@longpress="drag.longpress">
					<image class="icon-yd" :src="$mAssetsPath.icon_yd"></image>
				</view> -->
				<image class="icon-xx" @click.stop="onClick(obj.id,obj.isOpen)" :src="$mAssetsPath.icon_xianxing2"></image>
			</view>
		</view>
		<view :class="{'uni-collapse-cell__content--hide':!isOpen}" class="uni-collapse-cell__content">
			<view :id="elId" :class="{ 'uni-collapse-cell--animation': showAnimation === true }" class="uni-collapse-cell__wrapper"
				:style="{'transform':isOpen?'translateY(0)':'translateY(-50%)','-webkit-transform':isOpen?'translateY(0)':'translateY(-50%)'}">
				<slot name="rowContent" :obj2="obj"></slot>
			</view>
		</view>
	</view>
</template>

<script src="./drag.wxs" module="drag" lang="wxs"></script>
<script>
	import uniIcons from '../uni-icons/uni-icons.vue'
	/**
	 * CollapseItem 折叠面板子组件
	 * @description 折叠面板子组件
	 * @property {String} title 标题文字
	 * @property {String} thumb 标题左侧缩略图
	 * @property {Boolean} disabled = [true|false] 是否展开面板
	 * @property {Boolean} showAnimation = [true|false] 开启动画
	 */
	import guid from '@/common/guid.js'
	export default {
		name: 'UniCollapseItem',
		components: {
			uniIcons
		},
		props: {
			rowHeight:{
				type: String,
				default: ''
			},
			listType:{
				// 唯一标识符
				type: Number,
				default: 0
			},
			obj:{
				type:Object,
				default () {
					return {
						
					}
				}
			},
			id:{
				// 唯一标识符
				type: Number,
				default: 0
			},
			title: {
				// 列表标题
				type: String,
				default: ''
			},
			name: {
				// 唯一标识符
				type: [Number, String],
				default: 0
			},
			disabled: {
				// 是否禁用
				type: Boolean,
				default: false
			},
			showAnimation: {
				// 是否显示动画
				type: Boolean,
				default: false
			},
			open: {
				// 是否展开
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				isOpen: false,
				height: 0, // body内容的高度
				elId: guid(),
			}
		},
		watch: {
			open(val) {
				this.isOpen = val
			}
		},
		inject: ['collapse'],
		created() {
			this.isOpen = this.open
			this.nameSync = this.name ? this.name : this.collapse.childrens.length
			this.collapse.childrens.push(this)
			
			if (String(this.collapse.accordion) === 'true') {
				if (this.isOpen) {
					let lastEl = this.collapse.childrens[this.collapse.childrens.length - 2]
					console.log(33,lastEl)
					if (lastEl) {
						this.collapse.childrens[this.collapse.childrens.length - 2].isOpen = false
					}
				}
			}
		},
		methods: {
			//增加
			onClickAdd(id){
				this.$emit("onClickAdd",id)
			},
			//编辑
			onClickEdit(item){
				console.log(9999,item)
				this.$emit("onClickEdit",item)
			},
			//删除
			onClickDel(id){
				this.$emit("onClickDel",id)
			},
			onClick(id,isOpen) {
				if (this.disabled) {
					return
				}
				if (String(this.collapse.accordion) === 'true') {
					this.collapse.childrens.forEach(vm => {
						if (vm === this) {
							return
						}
						vm.isOpen = false
					})
				}
				this.isOpen = !this.isOpen
				this.$emit("toClick",id,this.isOpen)
				this.collapse.onChange && this.collapse.onChange()
				this.queryRect();
				this.$forceUpdate();
			},
			// 查询内容高度
			queryRect() {
				const query = uni.createSelectorQuery().in(this);
				query
					.select('#' + this.elId)
					.boundingClientRect(data => {
						// console.log(data.height)
						if (!data.height) {
							setTimeout(() => {
								this.queryRect();
							}, 100);
							return;
						}
						this.height = data.height;
					})
					.exec();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.icons {
		display: flex;
	}
	
	.icon-yd {
		width: 46rpx;
		height: 46rpx;
		margin: 32rpx 0 32rpx 28rpx;
	}
	.icon-add {
		width: 36rpx;
		height: 36rpx;
	}
	.icon-bj {
		margin-left: 30rpx;
		width: 34rpx;
		height: 34rpx;
	}
	.icon-sc {
		margin-left: 30rpx;
		width: 32rpx;
		height: 34rpx;
	}
	.icon-xx {
		margin-left: 30rpx;
		width: 34rpx;
		height: 28rpx;
	}

	.uni-collapse-cell {
		flex-direction: column;
		/* border-color: #f5f5f5;
		border-bottom-width: 1rpx;
		border-bottom-style: solid; */
		background-color: #fff;
		margin-bottom: 16rpx;
	}

	.uni-collapse-cell--hover {
		background-color: #fff;
	}

	.uni-collapse-cell--open {
		background-color: #fff;
	}

	.uni-collapse-cell--disabled {
		background-color: #fff;
		/* #ifdef H5 */
		cursor: not-allowed !important;
		/* #endif */
	}

	.uni-collapse-cell--hide {
		height: 48px;
	}

	.uni-collapse-cell--animation {
		transition-property: transform;
		transition-duration: 0.3s;
		transition-timing-function: ease;
	}

	.uni-collapse-cell__title {
		padding: 0 30rpx;
		position: relative;
		/* #ifndef APP-NVUE */
		display: flex;
		width: 100%;
		box-sizing: border-box;
		/* #endif */
		height: 100rpx;
		line-height: 100rpx;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
		/* #ifdef H5 */
		cursor: pointer;
		/* #endif */

		.uni-collapse-cell__l {
			display: flex;
			width: calc(100% - 300rpx);
			overflow: hidden;
			text-overflow: ellipsis;
			white-space: nowrap;
		}
		.uni-collapse-cell__r {
			display: flex;
			align-items: center;
			width: 300rpx;
		}
	}

	.uni-collapse-cell__title:active {
		//background-color: #fff;
	}

	.uni-collapse-cell__title-text {
		flex: 1;
		font-size: 32rpx;
		color: $font-color-3;
		margin-right: 14rpx;
		/* #ifndef APP-NVUE */
		white-space: nowrap;
		color: inherit;
		/* #endif */
		/* #ifdef APP-NVUE */
		lines: 1;
		/* #endif */
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.uni-collapse-cell__content {
		overflow: hidden;
	}

	.uni-collapse-cell__wrapper {
		/* #ifndef APP-NVUE */
		display: flex;
		/* #endif */
		flex-direction: column;
	}

	.uni-collapse-cell__content--hide {
		height: 0px;
		line-height: 0px;
	}
</style>
